package demo2;

import java.util.Arrays;

public class Test {
    public static void bubbleSort(Comparable[] comparable) {
        for(int i=0;i< comparable.length-1;i++){
            for(int j=0;j< comparable.length-1-i;j++){
                if(comparable[j].compareTo(comparable[j+1])>0){//因为用的是Comparable接口，所有要调用compareTo
                    Comparable tmp=comparable[j];//tmp类型是Comparable
                    comparable[j]=comparable[j+1];
                    comparable[j+1]=tmp;
                }
            }
        }
    }
    public static void main(String[] args) {
        Person[] people=new Person[3];
        people[0]=new Person("qq",1,90.1);
        people[1]=new Person("ww",2,91.9);
        people[2]=new Person("zz",3,92.9);
        bubbleSort(people);//用冒泡排序来重写Array.sort
        //Arrays.sort(people);//想用这个必须使用Comparable接口,在Person类种实现了Comparable接口，并且用compareTo来比较年龄
        //输出结果一定是年龄按从小到大排序的
        System.out.println(Arrays.toString(people));
    }
    public static void main2(String[] args) {//这个利用了Comparator接口进行比较，更加灵活，不过需要注意的是名字是引用类型，不可直接比较
        //需要借助compareTo来进行比较
        Person person = new Person("qq", 2, 90.9);
        Person person1 = new Person("ww", 3, 91.9);
        AgeComparator ageComparator=new AgeComparator();
        int ret=ageComparator.compare(person,person1);
        {
            if(ret>0){
                System.out.println("年龄关系:p>p1");
            }else if(ret<0){
                System.out.println("年龄关系:p<p1");
            }else{
                System.out.println("年龄关系:p=p1");
            }
        }
        ScoreComparator scoreComparator=new ScoreComparator();
        ret=scoreComparator.compare(person,person1);{
            if(ret>0){
                System.out.println("分数关系:p>p1");
            }else if(ret<0){
                System.out.println("分数关系:p<p1");
            }else{
                System.out.println("分数关系:p=p1");
            }
        }
        NameComparator nameComparator=new NameComparator();
        ret=nameComparator.compare(person,person1);{
            if(ret>0){
                System.out.println("名字关系:p>p1");
            }else if(ret<0){
                System.out.println("名字关系:p<p1");
            }else{
                System.out.println("名字关系:p=p1");
            }
        }
    }

    public static void main1(String[] args) {//利用Comparable接口进行比较，这种比较是默认的，只能比较一种，因此这个比较不灵活
        //且如果相比较其他的，如分数，名字，还需要修改compareTo方法，因此这是受限制的
        Student student = new Student("qq", 2, 90.9);
        Student student1 = new Student("ww", 3, 91.9);
        if (student.compareTo(student1) > 0) {
            System.out.println("student>student1");
        } else if (student.compareTo(student1) < 0) {
            System.out.println("student<student1");
        } else {
            System.out.println("student=student1");
        }
    }
}
